package com.sccc.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport implements ServletContextAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1502392921110121006L;
	ServletContext application = null;
	private String title;
	private File fileupload;
	private String fileuploadContentType;
	private String fileuploadFileName;
	private String savePath;
	

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File getFileupload() {
		return fileupload;
	}

	public void setFileupload(File fileupload) {
		this.fileupload = fileupload;
	}


	public String getFileuploadContentType() {
		return fileuploadContentType;
	}

	public void setFileuploadContentType(String fileuploadContentType) {
		this.fileuploadContentType = fileuploadContentType;
	}

	public String getFileuploadFileName() {
		return fileuploadFileName;
	}

	public void setFileuploadFileName(String fileuploadFileName) {
		this.fileuploadFileName = fileuploadFileName;
	}

	public String getSavePath()throws Exception {
		return application.getRealPath(savePath);
	}

	public void setSavePath(String value) {
		this.savePath = value;
	}




	@Override
	public String execute() throws Exception {
		
		FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getFileuploadFileName());
		FileInputStream fis= new FileInputStream(getFileupload());
		System.out.println(getFileuploadFileName());
		System.out.println(getSavePath());
		
		byte[] buffer = new byte[1024];
		int len =0;
		while((len =fis.read(buffer))>0){
			
			fos.write(buffer,0,len);
		}
		fos.close();
		fis.close();
		
		return SUCCESS;
	}

	@Override
	public void setServletContext(ServletContext application) {
		this.application = application;
		
	}

}
